package draw

import (
	"image"
	"image/color"

	"gitee.com/yejia/radar/model"
	"gitee.com/yejia/radar/swan"
)

func DrawImg(colorJsonData [][]int, rw model.RadarWrapper) *image.RGBA {
	rsl := rw.ProductRasterParams.RowSideLength
	csl := rw.ProductRasterParams.ColumnSideLength
	data := rw.Data
	img := image.NewRGBA(image.Rect(0, 0, int(rsl), int(csl)))
	for y := range int(rsl) { // 纬度对应Y
		for x := range int(csl) { // 经度对应X
			v := data[y][x]
			if v != nil {
				img.Set(x, y, getColor(colorJsonData, *v))
			}
		}
	}
	return img
}
func DrawSwanImg(colorJsonData [][]int, rw swan.SwanModel) *image.RGBA {
	rsl := rw.SwanHeader.Y_grid_num
	csl := rw.SwanHeader.X_grid_num
	data := rw.Data
	img := image.NewRGBA(image.Rect(0, 0, int(rsl), int(csl)))
	for y := range int(rsl) { // 纬度对应Y
		for x := range int(csl) { // 经度对应X
			v := data[y][x]
			if v != nil {
				img.Set(x, y, getColor(colorJsonData, *v))
			}
		}
	}
	return img
}

func getColor(colorJsonData [][]int, value float64) color.Color {
	if value > 65 {
		cn := colorJsonData[len(colorJsonData)-1]
		return color.RGBA{uint8(cn[1]), uint8(cn[2]), uint8(cn[3]), 255}
	}
	for _, cjd := range colorJsonData {
		if value-5 < float64(cjd[0]) {
			return color.RGBA{uint8(cjd[1]), uint8(cjd[2]), uint8(cjd[3]), 255}
		}
	}
	return color.White
}
